<template>
	<div :class="curClassName" :style="curStyle">
		<a-select
			v-model="curValue"
			:class="editorClassName"
			placeholder="请选择组织"
			@blur="onBlur"
			@focus="onFocus"
		>
			<a-select-option
				v-for="(item, index) in items"
				:value="item.value"
				:key="item.value"
				>{{ item.label }}</a-select-option
			>
		</a-select>
	</div>
</template>
<script setup lang="ts">
import { LoginOrgProps } from './login-org';
import { Ref } from 'vue';
import { IEvent } from '@/core';

const props = defineProps(LoginOrgProps);

interface LoginOrgEmit {
	(name: 'component-action', value: IEvent<string>): void;
}
const emit = defineEmits<LoginOrgEmit>();

/**
 * 项名称
 *
 * @memberof AppLoginOrg
 */
const itemName = computed(() => {
	return props.index || props.index === 0
		? `${props.name}_${props.index}`
		: props.name;
});

/**
 * 当前容器样式
 *
 * @memberof AppLoginOrg
 */
const curStyle = computed(() => {
	const layoutModel = props.layoutModelDetails[itemName.value];
	if (layoutModel) {
		return layoutModel.getElementStyle();
	}
});

/**
 * 编辑器类名
 *
 * @memberof AppLoginOrg
 */
const editorClassName = computed(() => {
	const layoutModel = props.layoutModelDetails[itemName.value];
	if (layoutModel) {
		return layoutModel.getEditorDynaClass();
	}
});

/**
 * 类名
 *
 * @memberof AppLoginOrg
 */
const curClassName = computed(() => {
	const layoutModel = props.layoutModelDetails[itemName.value];
	const classNames = {
		'app-login-org': true,
	};
	if (layoutModel) {
		Object.assign(classNames, layoutModel.getClassNames());
	}
	return classNames;
});

/**
 * 当前值
 *
 * @type {any}
 * @memberof AppLoginInput
 */
const curValue = computed({
	get: () => props.value || '',
	set: (val) => {
		emit('component-action', {
			name: props.name,
			action: 'valueChange',
			data: val,
		});
	},
});

/**
 * 组织部门名称数组
 *
 * @type {Array<any>}
 * @memberof AppLoginOrg
 */
const items: Ref<Array<any>> = ref([]);

/**
 * 获取数据
 *
 * @memberof AppLoginOrg
 */
const getOrgData = () => {
	// todo
	console.log('获取数据暂未实现');
};

getOrgData();

/**
 * 聚焦
 * @param $event
 */
const onFocus = ($event: any) => {
	emit('component-action', {
		name: props.name,
		action: 'panelItemFocus',
		data: props.value,
	});
};

/**
 * 失焦
 * @param $event
 */
const onBlur = ($event: any) => {
	emit('component-action', {
		name: props.name,
		action: 'panelItemBlur',
		data: props.value,
	});
};
</script>
